<?php
require './include/common.inc.php';

$action = isset($_GET['action']) ? $_GET['action'] : '';
$page_title =   '问卷调查  --  '. $site_conf->get('SITE_NAME');
$css_file = 'complicate.css';
$time = date('Y-m-d');
switch($action)
{
    case 'show':
        $id = $_GET['id'];
        $db->prepare('select * from edufe_investigate where id=? and status=? and startdate<=? and enddate>=?');
        $db->set_string($id);
        $db->set_int(STATUS_NORMAL);
        $db->set_string($time);
        $db->set_string($time);
        $info = $db->execute(true);
        $db->prepare('select * from edufe_investigate_questions where investigateid=? and status=? order by listorder desc,inputtime asc');
        $db->set_string($id);
        $db->set_int(STATUS_NORMAL);
        $res_q = $db->execute();
        $db->prepare('select * from edufe_investigate_option where investigateid=? and status=? order by listorder desc,inputtime asc');
        $db->set_string($id);
        $db->set_int(STATUS_NORMAL);
        $res_i = $db->execute();
        $options_list = array();
        if(is_array($res_i))
        {
            foreach($res_i as $v)
            {
                if(!isset($options_list[$v['questionsid']]))
                {
                    $options_list[$v['questionsid']] = array();
                }
                $options_list[$v['questionsid']][] = $v;
            }
        }
        $questions_list = array();
        if(is_array($res_q))
        {
            foreach($res_q as $v)
            {
                $tmp = $v;
                $tmp['options'] = isset($options_list[$v['id']]) ? $options_list[$v['id']]:array();
                $questions_list[] = $tmp;
            }
        }
        $size = sizeof($questions_list);
        include template('default', 'vote_show');
        break;
    case 'result':
        $id = $_GET['id'];
        $db->prepare('select * from edufe_investigate where id=? and status=? and startdate<=? and enddate>=?');
        $db->set_string($id);
        $db->set_int(STATUS_NORMAL);
        $db->set_string($time);
        $db->set_string($time);
        $info = $db->execute(true);
        $db->prepare('select * from edufe_investigate_questions where investigateid=? and status=? order by listorder desc,inputtime asc');
        $db->set_string($id);
        $db->set_int(STATUS_NORMAL);
        $res_q = $db->execute();
        $db->prepare('select * from edufe_investigate_option where investigateid=? and status=? order by listorder desc,inputtime asc');
        $db->set_string($id);
        $db->set_int(STATUS_NORMAL);
        $res_i = $db->execute();
        $options_list = array();
        if(is_array($res_i))
        {
            foreach($res_i as $v)
            {
                if(!isset($options_list[$v['questionsid']]))
                {
                    $options_list[$v['questionsid']] = array();
                }
                $options_list[$v['questionsid']][] = $v;
            }
        }
        $questions_list = array();
        if(is_array($res_q))
        {
            foreach($res_q as $v)
            {
                $tmp = $v;
                $tmp['options'] = isset($options_list[$v['id']]) ? $options_list[$v['id']]:array();
                $questions_list[] = $tmp;
            }
        }
        $size = sizeof($questions_list);
        include template('default', 'vote_result');
        break;
    case 'vote':
        $id = $_GET['id'];
        $db->prepare('select count(*) as nums from edufe_investigate where id=? and status=? and startdate<=? and enddate>=?');
        $db->set_string($id);
        $db->set_int(STATUS_NORMAL);
        $db->set_string($time);
        $db->set_string($time);
        $info = $db->execute(true);
        if($info['nums'] <= 0)
        {
            $response->alert('您参与的问卷调查不存在或已经过期');
            $response->execute();
        }
        if(isset($_POST['an']))
        {
            if(is_array($_POST['an']) && sizeof($_POST['an']))
            {
                foreach($_POST['an'] as $qid=>$v)
                {
                    $db->simple_update('edufe_investigate_option',array('status'=>STATUS_NORMAL),array('id'=>$v),'num=num+1');
                }
            }
            $response->alert('投票成功，感谢您参与此次投票');
            $response->redirect('/vote.php?action=result&id='.$id);
            $response->execute();
        }
        else
        {
            $response->alert('请至少参与一项投票');
            $response->execute();
        }
        break;
    default:
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;
        $page = max($page,1);
        $pagesize = 28;
        $start = ($page - 1) * $pagesize;
        $time = date('Y-m-d');
        $db->prepare('select count(*) as nums from edufe_investigate where status=? and siteid=? and startdate<=? and enddate>=?');
        $db->set_int(STATUS_NORMAL);
        $db->set_string('master');
        $db->set_string($time);
        $db->set_string($time);
        $res_count = $db->execute(true);
        $count = $res_count['nums'];
        $list = array();
        $total = 0;
        if($count > 0)
        {
            $total = ceil($count / $pagesize);
            $db->prepare('select * from edufe_investigate where status=? and siteid=? and startdate<=? and enddate>=? order by listorder desc,inputtime desc limit ?,?');
            $db->set_int(STATUS_NORMAL);
            $db->set_string('master');
            $db->set_string($time);
            $db->set_string($time);
            $db->set_int($start);
            $db->set_int($pagesize);
            $list = $db->execute();
        }
        include template('default', 'vote_list');
}
?>
